<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue 获取通过 URL 传递过来的参数</title>
</head>
<body>
<div id="app">
    <!--  router-link 在使用过程中，可以实现在不发生网络情况和刷新页面的情况下，实现页面的切花  -->
    <router-link to="/">首页</router-link>
    <router-link to="/user/1">用户1</router-link>
    <router-link to="/user/2">用户2</router-link>
    <router-link to="/user/3?name=abcque&gender=m&age=18">用户3</router-link>
    <router-link to="/about">关于</router-link>
    <router-view></router-view>
</div>

<template id="user">
    <h1>User {{ $route.params.id }} Page</h1>
    <h2 v-if="JSON.stringify($route.query) != '{}'">{{ $route.query }}</h2>
</template>

<script type="module">
    import {createApp, defineComponent} from "../../../../js/vue.esm-browser.js";
    import {createRouter, createWebHashHistory} from "../../../../js/vue-router.esm-browser.js";

    // step 1.定义组件
    const Home = {template: "<h1>Home Page</h1>"}

    const About = {template: "<h1>About Page</h1>"}

    // const User = {template: "<h1>User Page</h1>"}
    // 使用  $route.params.标识符 访问接收到的当前用户 ID
    // const User = {template: "<h1>User {{ $route.params.id }} Page</h1>"}

    let User = defineComponent(
        {
            template:"#user",
        }
    );


    // step 2.定义组件和 URL 的对应关系
    const routes = [
        {
            path: "/",
            component: Home,
        },
        {
            path: "/about",
            component: About,
        },
        /*{
            path: "/user/1",
            component: User,
        },

        {
            path: "/user/2",
            component: User,
        },*/
        {
            // : 代表的是动态路由，后面可以跟上一个标识符，用来接收对应位置的 url 部分内容
            path: "/user/:id",
            component: User,
        },
    ]


    const router = createRouter(
        {
            history: createWebHashHistory(),
            routes: routes,
        }
    )

    let app = createApp(
        {
            setup() {
                return {}
            }
        }
    )

    app.use(router)
    app.mount("#app")
</script>
</body>
</html>